package com.own.business.resource.controller;

import com.own.business.resource.business.service.FileRecordService;
import com.own.business.resource.entity.dto.FileRecordDto;
import com.own.business.resource.entity.query.FileRecordQuery;
import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.util.list.ListUtil;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * FileRecordController
 *
 * @author chenxueli
 * @date 2023-07-07 14:48:53
 */
@Tag(name = "文件记录" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "file/record")
public class FileRecordController extends BaseController {

    @Resource
    private FileRecordService fileRecordService;

    @LogOperationAop
    @Operation(summary = "分页查询文件信息")
    @PostMapping(value = "/page")
    public ResultModel<PageModel<FileRecordVo>> page(@RequestBody FileRecordQuery query) {
        var pageModel = fileRecordService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @Operation(summary = "保存文件信息")
    @PostMapping(value = "/")
    public ResultModel<FileRecordVo> add(@RequestBody FileRecordDto dto) {
        dto.check();
        var vo = fileRecordService.add(dto.toBo());
        return R.success(vo);
    }

    @LogOperationAop
    @Operation(summary = "根据文件id获取文件信息")
    @GetMapping(value = "/{fileId}")
    public ResultModel<FileRecordVo> getById(
            @Parameter(description = "文件id信息", required = true) @PathVariable Long fileId
    ) {
        var vo = fileRecordService.getById(fileId);
        return R.success(vo);
    }

    @LogOperationAop
    @Operation(summary = "删除文件信息")
    @DeleteMapping(value = "/{fileId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "文件id信息", required = true) @PathVariable Long fileId
    ) {
        var flag = fileRecordService.delete(fileId);
        return R.success(flag);
    }

    @LogOperationAop
    @Operation(summary = "批量删除文件信息")
    @DeleteMapping(value = "/batch/{fileIds}")
    public ResultModel<Boolean> batchDelete(
            @Parameter(description = "文件ids信息", required = true) @PathVariable String fileIds
    ) {
        var fileIdList = ListUtil.Spilt.toLong(fileIds, ",");
        var flag = fileRecordService.deleteByIds(fileIdList);
        return R.success(flag);
    }

}
